﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DALC.AcessoDALC;
using System.Data;
using System.Web.Services;

namespace Roteirização
{
    public partial class Motoristas : System.Web.UI.Page
    {


        [WebMethod] 
        public static Motorista[] GetMotoristas()
        {
            ConexaoDALC con = new ConexaoDALC();        
            List<Motorista> list = new List<Motorista>();
            DataTable dt =  con.RetornaDataTable("select * from motoristas;", CommandType.Text)  ;
            foreach (DataRow row in dt.Rows)
            {
                Motorista Moto = new Motorista();
                Moto.Nome = row["Nome"].ToString();
                Moto.CPF = row["CPF"].ToString();

                list.Add(Moto);
            }
            return list.ToArray();
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            //if(!IsPostBack)
            //CarregarTela();
        }

        protected void CarregarTela()
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[] { new DataColumn("Nome"), new DataColumn("CPF") });
            Gridv.DataSource = GetMotoristas();
            Gridv.DataBind();
        }


        protected void SalvarMotorista()
        {
            String str;
            ConexaoDALC con = new ConexaoDALC();
            

            str = "insert into motoristas values(DEFAULT,'" + txtNome.Value + "',''," + txtRG.Value + ",'" + txtCPF.Value + "'," + txtCNH.Value + ",'" + txtCatCNH.Value + "','" + txtRuaM.Value + "'," + txtNumM.Value + ",'" + 
                txtCompM.Value + "','" + txtBairroM.Value + "','" + txtCidadeM.Value + "','" + txtEstadoM.Value + "','" + txtCEPM.Value + "','" + txtPaisM.Value + "');";

            con.ExecuteNonQuery(str, System.Data.CommandType.Text);


        }

        protected void btnSalvarM_Click(object sender, EventArgs e)
        {
            SalvarMotorista();
            CarregarTela();
        }
    }
}